package com.Register.config;

import com.Register.interceptor.JwtTokenAppletInterceptor;
import com.Register.interceptor.JwtTokenWebAdminInterceptor;
import com.Register.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private JwtTokenWebAdminInterceptor jwtTokenWebInterceptor;
    @Autowired
    private JwtTokenAppletInterceptor jwtTokenAppletInterceptor;

    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenWebInterceptor)
                .addPathPatterns("/web/admin/**")
                .excludePathPatterns("/web/admin/updatePassword")
                .excludePathPatterns("/web/admin/login");
        registry.addInterceptor(jwtTokenAppletInterceptor)
                .addPathPatterns("/applet/user/**")
                .excludePathPatterns("/applet/user/login")
                .excludePathPatterns("/applet/user/carousel/active")
                .excludePathPatterns("/applet/user/activity/activityPage")
                .excludePathPatterns("/applet/user/skill/getActive")
                .excludePathPatterns("/applet/user/wxLogin")
                .excludePathPatterns("/applet/user/bind");
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 扩展消息转换器,将日期类型从列表转换为时间戳
     * 这个是导致knife4j不能正常显示的罪魁祸首,特别要注意添加的位置
     * @param converters 消息转换器列表
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("开始扩展消息转换器...");
        //创建一个消息转换器对象
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        //需要为消息转换器设置一个对象转换器，对象转换器可以将java对象序列化为json数据
        jackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
        //将消息转换器对象添加到容器中
        //注意添加的位置，这个是关键,如果是converters.add(0,converter);，则默认第一个就来，json数据进行转换，从而导致swagger不能正常显示
        converters.add(converters.size()-1,jackson2HttpMessageConverter);
    }
}
